package org.cy.websocket.stomp.client;

import org.cy.websocket.stomp.StompClient;
import org.cy.websocket.stomp.StompClientBuilder;
import org.cy.websocket.stomp.StompHeaders;
import org.cy.websocket.stomp.messaging.StringMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created in 2021-05-13 14:27
 *
 * @author cao.yong
 */
@RestController
@SpringBootApplication
public class StompClientApplication {
    private static final Logger LOG = LoggerFactory.getLogger(StompClientApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(StompClientApplication.class, args);
    }

    @Bean
    public StompClient stompClient() {
        StompHeaders headers = new StompHeaders();
        headers.add("deviceName", "02");
        headers.add("deviceType", "0");
        headers.add("deviceInit", "true");
        headers.add("devicePair", "");
        return new StompClientBuilder().url("http://localhost:4201/comm")
                .heartbeat(0, 0)
                .connectHeaders(headers)
                .disconnectHeaders(headers)
                .onConnected(message -> {
                    String body = StringMessageConverter.fromMessage(message);
                    LOG.info("message: {}, {}", message.getHeaders(), body);
                })
                .build();
    }
}
